package qianfeng.com.meijiadaka.adapter.shop_details;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import qianfeng.com.meijiadaka.R;
import qianfeng.com.meijiadaka.bean.detail.SpecialBeanAbove;
import qianfeng.com.meijiadaka.bean.detail.SpecialBeanBelow;
import qianfeng.com.meijiadaka.utils.MyImageview;

/**
 * Created by Think on 2017/2/9.
 */

public class SpecialDetailsAdapter extends BaseAdapter {
    private Context context;
    private List<Object>  lists;
    private List<Object> list;
    private SpecialBeanAbove s1;
    private SpecialBeanBelow s2;

        public SpecialDetailsAdapter(Context context, List<Object> list) {
        this.context = context;
        this.list = list;
        lists = new ArrayList<>();
        s1 = new SpecialBeanAbove();
        s2 = new SpecialBeanBelow();
//        s1 = (SpecialBeanAbove) list.get(0);
//        s2 = (SpecialBeanBelow) list.get(1);
            Log.e("SpecialDetailsAdapterLIst: ", list.size()+"" );
            if (list.get(0) instanceof  SpecialBeanAbove){
                if (((SpecialBeanAbove) list.get(0)).getResult().getDesc().size() > 0) {
                    lists.add(((SpecialBeanAbove) list.get(0)).getResult().getDesc());
                }
                if (((SpecialBeanAbove) list.get(0)).getResult().getCommodities().size() > 0) {
                lists.add(((SpecialBeanAbove) list.get(0)).getResult().getCommodities());
                }
                if (((SpecialBeanAbove) list.get(0)).getResult().getRelated().size() > 0) {
                    lists.add(((SpecialBeanAbove) list.get(0)).getResult().getRelated());
                }
            }
            if(list.get(1) instanceof  SpecialBeanBelow){
                if (((SpecialBeanBelow) list.get(1)).getResult().getComments().size() > 0) {
                    lists.add(((SpecialBeanBelow) list.get(1)).getResult().getComments());
                }
            }
            Log.e("SpecialDetailsAdapter: ", lists.size()+"" );
    }



    @Override
    public int getItemViewType(int position) {

        if (lists.get(position).equals(((SpecialBeanAbove) list.get(0)).getResult().getDesc())) {
            return 0;
        } else if (lists.get(position).equals(((SpecialBeanAbove) list.get(0)).getResult().getCommodities())) {
            return 1;
        } else if (lists.get(position).equals(((SpecialBeanAbove) list.get(0)).getResult().getRelated())) {
            return 2;
        } else {
            return 3;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public Object getItem(int position) {
        return lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        ViewHolder1 viewHolder1 = null;
        ViewHolder2 viewHolder2 = null;
        ViewHolder3 viewHolder3 = null;

        int type = getItemViewType(position);

        if (convertView == null) {
            switch (type) {
                case 0:
                    convertView = LayoutInflater.from(context).inflate(R.layout.specia_details_list_item1, parent, false);
                    viewHolder = new ViewHolder();
                    viewHolder.iv = (ImageView) convertView.findViewById(R.id.specia_list1_iv);
                    viewHolder.tv = (TextView) convertView.findViewById(R.id.specia_list1_tv2);
                    convertView.setTag(viewHolder);
                    break;
                case 1:
                    convertView = LayoutInflater.from(context).inflate(R.layout.specia_details_list_item2, parent, false);
                    viewHolder1 = new ViewHolder1();
                    viewHolder1.rv = (RecyclerView) convertView.findViewById(R.id.specia_details_list_item2_recyclerView);
                    convertView.setTag(viewHolder1);
                    break;
                case 2:
                    convertView = LayoutInflater.from(context).inflate(R.layout.specia_details_list_item3, parent, false);
                    viewHolder2 = new ViewHolder2();
                    viewHolder2.iv = (MyImageview) convertView.findViewById(R.id.specia_list2_iv);
                    viewHolder2.iv1 = (ImageView) convertView.findViewById(R.id.specia_list2_iv1);
                    viewHolder2.iv2 = (ImageView) convertView.findViewById(R.id.specia_list2_iv2);
                    viewHolder2.iv3 = (ImageView) convertView.findViewById(R.id.specia_list2_iv3);
                    viewHolder2.tv1 = (TextView) convertView.findViewById(R.id.specia_list2_tv1);
                    viewHolder2.tv2 = (TextView) convertView.findViewById(R.id.specia_list2_tv2);
                    convertView.setTag(viewHolder2);
                    break;
                case 3:
                    convertView = LayoutInflater.from(context).inflate(R.layout.specia_details_list_item4, parent, false);
                    viewHolder3 = new ViewHolder3();
                    viewHolder3.rv = (RecyclerView) convertView.findViewById(R.id.specia_details_list_item4_commend_recyclerView);
                    viewHolder3.tv = (TextView) convertView.findViewById(R.id.specia_list4_tv_comment);
                    viewHolder3.et = (EditText) convertView.findViewById(R.id.specia_list4_edit_comment);
                    convertView.setTag(viewHolder3);
                    break;
            }
        } else {
            switch (type) {
                case 0:
                    viewHolder = (ViewHolder) convertView.getTag();
                    break;
                case 1:
                    viewHolder1 = (ViewHolder1) convertView.getTag();
                    break;
                case 2:
                    viewHolder2 = (ViewHolder2) convertView.getTag();
                    break;
                case 3:
                    viewHolder3 = (ViewHolder3) convertView.getTag();
                    break;
            }
        }

        Log.e("type: ", "==" + type);
        switch (type) {
            case 0:
                Log.e("getView: ", "===========0000=======");
                if (lists.get(position).equals(((SpecialBeanAbove) list.get(0)).getResult().getDesc())) {
                    viewHolder.tv.setText(((SpecialBeanAbove) list.get(0)).getResult().getDesc().get(0).getArticle());
                    Picasso.with(context).load(((SpecialBeanAbove) list.get(0)).getResult().getDesc().get(0).getPicture()).into(viewHolder.iv);
                }
                break;
            case 1:
                Log.e("getView: ","===========1111=======" );
                if (lists.get(position).equals(((SpecialBeanAbove) list.get(0)).getResult().getCommodities())){

                    List<SpecialBeanAbove.ResultBean.CommoditiesBean> cb = ((SpecialBeanAbove) list.get(0)).getResult().getCommodities();
                    SpecialDetailsLvItem2Adapter adapter = new SpecialDetailsLvItem2Adapter(context,cb);
                    LinearLayoutManager manager = new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);
                    viewHolder1.rv.setLayoutManager(manager);
                    viewHolder1.rv.setAdapter(adapter);
                }

                break;
            case 2:
                Log.e("getView: ","===========2222=======" );
                if (lists.get(position).equals(((SpecialBeanAbove) list.get(0)).getResult().getRelated()) &&((SpecialBeanAbove) list.get(0)).getResult().getRelated().size() > 0){
                    viewHolder2.tv1.setText(((SpecialBeanAbove) list.get(0)).getResult().getDetail().getAuthorName());
                    viewHolder2.tv2.setText("教程"+((SpecialBeanAbove) list.get(0)).getResult().getTotalTutorials()+"个");
                    Picasso.with(context).load(((SpecialBeanAbove) list.get(0)).getResult().getDetail().getAvatar())
                            .into(viewHolder2.iv);
                    Picasso.with(context).load(((SpecialBeanAbove) list.get(0)).getResult().getRelated().get(0).getThumbUrl()).into(viewHolder2.iv1);
                    Picasso.with(context).load(((SpecialBeanAbove) list.get(0)).getResult().getRelated().get(1).getThumbUrl()).into(viewHolder2.iv2);
                    Picasso.with(context).load(((SpecialBeanAbove) list.get(0)).getResult().getRelated().get(2).getThumbUrl()).into(viewHolder2.iv3);
                }
                break;
            case 3:
                viewHolder3.tv.setText("评论 ("+((SpecialBeanAbove) list.get(0)).getResult().getCommentCount()+")");

                LinearLayoutManager manager = new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);
                viewHolder3.rv.setLayoutManager(manager);
                SpecialDetailsCommend1Adapter adapter = new SpecialDetailsCommend1Adapter((SpecialBeanBelow) list.get(1),context);
                viewHolder3.rv.setAdapter(adapter);
                break;
        }
        return convertView;
    }


    class ViewHolder {
        ImageView iv;
        TextView tv;
    }

    class ViewHolder1 {
        RecyclerView rv;
    }

    class ViewHolder2 {
        ImageView iv,iv1,iv2,iv3;
        TextView tv1,tv2;
    }

    class ViewHolder3 {
        TextView tv;
        EditText et;
        RecyclerView rv;
    }


}
